<template>
  <div class="login-wrapper">
    <div class="login-form">
      <h2 class="login-title">梦学谷会员管理系统</h2>
      <el-form
        :model="loginForm"
        :rules="rules"
        ref="loginForm"
        label-width="60px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input v-model.trim="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model.trim="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import userApi from "../../api/user";
export default {
  name: "",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm() {
      this.$refs["loginForm"].validate((valid) => {
        if (valid) {
          this.$store.dispatch("login",this.loginForm).then(response=>{
            if(response.data.flag){
               this.$message({message: response.data.message,type: 'success'});
              this.$store.dispatch("getUserInfo").then(response=>{
                if(response.data.flag){
                   this.$message({message: response.data.message,type: 'success'});
                  this.$router.push("/")
                }else{
                  this.$message({message: response.data.message,type: 'error'});
                }
              })
            }else{
              this.$message({message: response.data.message,type: 'error'});
            }
          })
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.login-wrapper {
  background: url("../../assets/login.jpg") no-repeat left;
  height: 100%;
  width: 100%;

  .login-form {
    width: 350px;
    background: rgba(255, 255, 255, 0.8);
    position: absolute;
    top: 160px;
    left: 50%;
    margin-left: -175px;
    padding: 30px;
    border-radius: 15px;

    .login-title {
      font-size: 18px;
      font-weight: bold;
      text-align: center;
      color: #303133;
      margin-bottom: 20px;
    }
  }
}
</style>
